import 'dart:math';

import 'package:car_music/src/commont/button.dart';
import 'package:car_music/src/modules/sum/my_sum_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';

import '../../commont/kcontainer.dart';
import 'output_control.dart';

class MySum extends SumPageDelegate<MySumController> {
  MySum({super.key});

  @override
  void onInit(BuildContext context) {
    super.onInit(context);
  }

  Widget sumItem(int index) {
    return kMyTextButton(
        name: controller.sumList[index],
        select: controller.sumIndex.value == index,
        onClick: (p) => controller.chooseSum(index));
  }

  @override
  Widget buildBody(BuildContext context, Widget child) {
    Get.lazyPut(() => MySumController());
    return Column(
      children: [
        OutputDbControl(),
        const SizedBox(height: 10),

        ///input mode
        kExpanded(
          flex: 2,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ///文字部分
              Expanded(
                  flex: 3,
                  child: Container(
                      alignment: Alignment.center,
                      child: const Text('INPUT MODE',
                          style: TextStyle(color: Colors.white), textAlign: TextAlign.center))),

              ///按钮部分
              Expanded(
                  flex: 8,
                  child: Container(
                    padding: const EdgeInsets.only(left: 20, right: 20, bottom: 15),
                    child: Obx(() {
                      double space = 15;
                      return Column(
                        spacing: space,
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          ///第一行2个
                          Flexible(
                            child: Row(
                              spacing: space,
                              crossAxisAlignment: CrossAxisAlignment.stretch, // 关键
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: controller.sumList
                                  .take(2)
                                  .toList()
                                  .asMap()
                                  .keys
                                  .map(($0) => Expanded(child: sumItem($0)))
                                  .toList(),
                            ),
                          ),

                          ///剩下的用gridview,每行3个
                          Flexible(
                            child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
                              double width = constraints.maxWidth;
                              double height = constraints.maxHeight;
                              int length = min(controller.sumList.length - 2, 3);
                              double gap = space;
                              double ratio = (width - (length - 1) * gap) / length / (height);
                              return GridView.builder(
                                  // physics: const NeverScrollableScrollPhysics(),
                                  padding: EdgeInsets.zero,
                                  itemCount: controller.sumList.length - 2,
                                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                                      crossAxisCount: length, //一行最多3个
                                      crossAxisSpacing: gap,
                                      mainAxisSpacing: 0,
                                      childAspectRatio: ratio),
                                  itemBuilder: (BuildContext context, index) => sumItem(index + 2));
                            }),
                          )
                        ],
                      );
                    }),
                  )),
            ],
          ),
        ),
      ],
    );
  }
}
